﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ConfeccionesJR.Entites;
using ConfeccionesJR.BussinesLogic;
using Microsoft.Practices.Unity;

namespace ConfeccionesJR.UI
{
    public partial class frmMaterial : Form
    {
        [Dependency]
        public IMaterialBL materialBL { get; set; }

        [Dependency]
        public IImpuestoBL impuestoBL { get; set; }

        public frmMaterial()
        {
            InitializeComponent();
            this.BuildUp();
        }

        private void frmMaterial_Load(object sender, EventArgs e)
        {
            mATERIALBindingSource.DataSource = materialBL.GetAllFromMaterial();
        }

        private void BtnBuscar_Click(object sender, EventArgs e)
        {
            if (rbtnGeneral.Checked == true)
            {
                BuscarPorDescripcion();
            }

            if (rbtnStock.Checked == true)
            {
                BuscarPorStock();
            }
        }

        private void BuscarPorDescripcion()
        {
            //if (TxtDescripcion.Text.Length > 0)
            //{
                mATERIALBindingSource.DataSource = materialBL.GetFromMaterialByDescripcion(TxtDescripcion.Text);
            //}

        }
        private void BuscarPorStock()
        {
            //if (TxtDescripcion.Text.Length > 0)
            //{
                var _minimo = 5; // Convert.ToInt32(impuestoBL.GetFromImpuestoById(5).Porcentaje);
                mATERIALBindingSource.DataSource = materialBL.GetAllFromMaterialsByDescripcionAndStockBajo(TxtDescripcion.Text, _minimo);
            //}
        }

        private void TxtDescripcion_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (rbtnGeneral.Checked == true)
                {
                    BuscarPorDescripcion();
                }

                if (rbtnStock.Checked == true)
                {
                    BuscarPorStock();
                }
            }
        }

        private void BtnAdd_Click(object sender, EventArgs e)
        {
            frmMttoMaterial frm = new frmMttoMaterial();
            frm.Nuevo();
            frm.ShowDialog();
            mATERIALBindingSource.DataSource = materialBL.GetAllFromMaterial();   
        }

        private void BtnUpdate_Click(object sender, EventArgs e)
        {
            MATERIAL currentMaterial = mATERIALBindingSource.Current as MATERIAL;

            if (currentMaterial != null)
            {
                frmMttoMaterial frm = new frmMttoMaterial();

                frm.IdMat = currentMaterial.Id;
                frm.Editar(currentMaterial.Id);
                frm.ShowDialog();
            }
            else
            {
                MessageBox.Show("Debe seleccionar un producto", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            mATERIALBindingSource.DataSource = materialBL.GetAllFromMaterial();

        }

        private void rbtnGeneral_Click(object sender, EventArgs e)
        {
            BuscarPorStock();
        }

        private void rbtnStock_Click(object sender, EventArgs e)
        {
            BuscarPorDescripcion();
        }

        private void TxtDescripcion_KeyPress(object sender, KeyPressEventArgs e)
        {
            var guion = Convert.ToString("-");
            var slash = Convert.ToString(",");
            var punto = Convert.ToString(".");
            string cadena = guion + slash + punto + (char)8;

            if (!cadena.Contains(e.KeyChar) && !(char.IsLetter(e.KeyChar)) && !(char.IsNumber(e.KeyChar))
                && (e.KeyChar != (char)Keys.Back) && (e.KeyChar != (char)Keys.Enter) && (e.KeyChar != (char)Keys.Clear)
                && !(char.IsSeparator(e.KeyChar)))
            {
                e.Handled = true;
                return;
            }
        }

        private void mATERIALDataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            try
            {
                for (int count = 0; count < mATERIALDataGridView.Rows.Count; count++)
                {
                    var montoMinimo = Convert.ToInt32(impuestoBL.GetFromImpuestoById(5).Porcentaje);

                    if (Convert.ToInt32(mATERIALDataGridView.Rows[count].Cells[2].Value) >= montoMinimo)
                    {
                        mATERIALDataGridView[2, count].Style.BackColor = System.Drawing.Color.Green; //SOLO LA COLUMNA
                    }

                    if (Convert.ToInt32(mATERIALDataGridView.Rows[count].Cells[2].Value) < montoMinimo
                        && Convert.ToInt32(mATERIALDataGridView.Rows[count].Cells[2].Value) > 0)
                    {
                        mATERIALDataGridView[2, count].Style.BackColor = System.Drawing.Color.Yellow;
                    }

                    if (Convert.ToInt32(mATERIALDataGridView.Rows[count].Cells[2].Value) == 0)
                    {
                        mATERIALDataGridView[2, count].Style.BackColor = System.Drawing.Color.Red;
                    }
                    else
                    {
                        mATERIALDataGridView.Rows[count].DefaultCellStyle.BackColor = System.Drawing.Color.White;
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Revise sus reglas de negocio");
            }
        }
    }
}
